package com.cw.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.cw.subject.common.enums.IsDeletedFlagEnum;
import com.cw.subject.domain.convert.SubjectCatetoryConverter;
import com.cw.subject.domain.entity.SubjectCategoryBO;
import com.cw.subject.domain.service.SubjectCategoryDomainService;
import com.cw.subject.infra.basic.entity.SubjectCategoryPO;
import com.cw.subject.infra.basic.service.SubjectCategoryPOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    // 引入infra中的SubjectCategoryService
    @Resource
    private SubjectCategoryPOService subjectCategoryPOService;

    @Override
    public Boolean add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectCategoryBO));
        }
        // (BO -> PO)
        SubjectCategoryPO subjectCategoryPO = SubjectCatetoryConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO); // 通过BO转化为PO

        subjectCategoryPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode()); // 设置is_deleted字段为0(未删除)
        int count = subjectCategoryPOService.insert(subjectCategoryPO);
        return count > 0;
    }

    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        // (BO -> PO)
        SubjectCategoryPO subjectCategoryPO = SubjectCatetoryConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);

        // 查询未删除的结果(该条件会存在where条件内)
        subjectCategoryPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectCategoryPO> subjectCategoryPOList = subjectCategoryPOService.queryCategory(subjectCategoryPO);

        // POList -> BOList
        List<SubjectCategoryBO> boList = SubjectCatetoryConverter.INSTANCE
                .convertPOListToCategoryBOList(subjectCategoryPOList);

        if (log.isInfoEnabled()) {
            log.info("SubjectCatetoryController.queryPrimaryCategory.boList:{}", JSON.toJSONString(boList));
        }
        return boList;
    }

    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        // (BO -> PO)
        SubjectCategoryPO subjectCategoryPO = SubjectCatetoryConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        int count = subjectCategoryPOService.update(subjectCategoryPO);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        // (BO -> PO)
        SubjectCategoryPO subjectCategoryPO = SubjectCatetoryConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategoryPO.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode()); // 逻辑删除
        // 使用update方法的SQL即可完成操作
        int count = subjectCategoryPOService.update(subjectCategoryPO);
        return count > 0;
    }

}
